/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.jessyan.lifecyclemodel.demo.slice;

import me.jessyan.lifecyclemodel.demo.MyApplication;
import me.jessyan.lifecyclemodel.demo.ResourceTable;
import me.jessyan.lifecyclemodel.LifecycleModelProviders;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * ================================================
 * BFraction 通过 {@link UserLifecycleModel#doAction(String)} 与其他组件开始通讯, (AFraction 与 BFraction 不存在耦合关系即可通讯以及共享数据)
 * ================================================
 */
public class BFraction extends Fraction {
    private static int id;
    private final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP, 0x00201, "Fraction");

    public static BFraction createBAbilitySlice(FractionManager fractionManager, int i) {
        BFraction fraction = new BFraction();
        fractionManager.startFractionScheduler().add(i, fraction).submit();
        id = i;
        return fraction;
    }

    /**
     * 获取BFraction的xml
     **/
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component;
        component = scatter.parse(ResourceTable.Layout_fraction_b, null, false);
        return component;
    }

    /**
     * 初始化
     **/
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    /**
     * 将参数传到A
     **/
    public void sendFraction() {
        UserLifecycleModel lifecycleModel = LifecycleModelProviders.of(getFractionAbility(), id).get(UserLifecycleModel.class.getName());
        lifecycleModel.doAction("parameters");
        MyApplication application = (MyApplication) getFractionAbility().getAbilityPackage();
        new ToastDialog(application.getApplicationContext()).setText("B将参数parameters传到A").show();
        HiLog.info(lable, "B将参数parameters传到A");
    }
}
